package gcontent.entities;

import gcontent.config.PersistentConfig;
import gcontent.config.persistents.node.ObjectNode;
import gcontent.db.DBParams;
import gcontent.entities.content.error.ContentEx;
import gcontent.entities.site.SiteURL;
import gcontent.launch.SpiderThread;
import gcontent.record.db.RecordDB;
import gcontent.record.error.RecordEx;
import gcontent.rsrc.ThreadResources;

import java.util.ArrayList;
import java.util.HashMap;

public class Site {
	
	protected HashMap<String, String> info_access = null;
	protected String protocol;
	protected long id;
	protected long id_tmp;
	
	/* database access */
	protected RecordDB record;
	protected SpiderThread spider;
	
	public Site()
	{
		this.spider = (SpiderThread)ThreadResources.getResource(ThreadResources.RSRC.SPIDER);
		if(this.spider != null)
			this.record = (RecordDB)this.spider.getRecord();
	}
	
	public HashMap<String, String> getInfoAccess()
	{
		return this.info_access;
	}
	
	public String getProtocol()
	{
		return this.protocol;
	}
	
	public void setProtocol(String protocol)
	{
		this.protocol = protocol;
	}
	
	public long getId()
	{
		return this.id;
	}
	
	public void setId(Long id)
	{
		this.id = id;
	}
	
	public void saveTMP() throws ContentEx, RecordEx 
	{	
		ObjectNode obj_node;
		
		obj_node = PersistentConfig.getConfPerClass(this.getClass().getName());
		if(obj_node == null)
			throw new RecordEx("not persistent config for class " +  this.getClass().getName());		
		
		this.id_tmp = (Integer)this.record.execProc("PROC_CreateRefTMP",
							this.id,
							obj_node.getId()
							);						
	}
	
	public static Site getTMP() throws ContentEx, RecordEx
	{
		RecordDB record;
		ArrayList<SiteURL> ret_site;
		
		record = (RecordDB)((SpiderThread)ThreadResources.getResource(ThreadResources.RSRC.SPIDER)).getRecord();
		
		ret_site = (ArrayList<SiteURL>)record.getObject("PROC_GetRefTmp");
		
		if(ret_site.size() > 0)
			return ret_site.get(0);
		
		return null;
	}
	
	public static void cleanTMP() throws ContentEx, RecordEx
	{
		RecordDB record;
		
		record = (RecordDB)((SpiderThread)ThreadResources.getResource(ThreadResources.RSRC.SPIDER)).getRecord();				
		record.execProc("PROC_CleanRefTmp");		
	}
}
